<?php
/**
 * 用户账户记录数据查询
 */
namespace app\common\query;

use app\common\model\MemberCoinRecord as MemberCoinRecordModel;

class MemberCoinRecord
{

	static $operate = [
				'transfer' => '会员转账',
				'convert' => '货币转换',
				'charge' => '充值',
				'withdraw' => '提现',
                'change' => '管理员增减',
                'active' => '激活会员',
                'buy' => '购物扣费',
                'rejected' => '购物驳回退费',
                'upgrade' => '原点升级',

                'lingshou' => '零售利润',
                'xiaoshou' => '销售补贴',
                'fuwu' => '服务补贴',
                'world' => '全体分红',
                'jiaquan' => '加权分红',

                'withdraw_refuse' => '拒绝提现',

			];
	static $flag = [
				'inc' => '增加',
				'dec' => '减少',
			];

	public static function operate($key=null){
		if(!is_null($key)){
			return self::$operate[$key];
		}else{
			return self::$operate;
		}
	}

	public static function flag($key=null){
		if(!is_null($key)){
			return self::$flag[$key];
		}else{
			return self::$flag;
		}
	}

	public function saveOneData($data,$id){
		$model = new MemberCoinRecordModel;
		$model -> allowField(true) -> save($data,['id'=>$id]);
	}

    /**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getRows($where=[],$queryStr='',$baseUrl){
        // 每页数量
        $perpagenum = $perpagenum = Option::get('perpage_default_num')?Option::get('perpage_default_num'):15;

        $rows = $this->getDatas($where,[],$perpagenum);

        $total = $rows['total'];
        $page = $rows['current_page'];
        $rows['page_html'] = pagination($total, $perpagenum, $page, $baseUrl, $queryStr);

        return $rows;
    }

    /**
     * 查询多条数据
     * @return [type] [description]
     */
	public function getDatas($where=[],$fields=[],$perpagenum=null){
        $model = new MemberCoinRecordModel;
        if($perpagenum){
            //dump($where);die;
            $data = $model->where($where)
            ->order('create_time','desc')->field($fields)->paginate($perpagenum);

            $data = $data->toArray();
            $rows = $data['data'];
            foreach($rows as &$row):
                $row = $this->setRemark($row);
            endforeach;
            $data['data'] = $rows;
        }else{
            $data = $model->where($where)->order('create_time','desc')->field($fields)->select();
            $data = $data->toArray();
            foreach($data as &$row):
                $row = $this->setRemark($row);
            endforeach;
        }

        return $data;
	}

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setRemark($row){
        if(isset($row['operate'])){
            $row['operate_remark'] = self::$operate[$row['operate']];
        }
        if(isset($row['flag'])){
            $row['flag_remark'] = self::flag($row['flag']);
        }
        if(isset($row['coin_id'])){
            $row['coin_name'] = Coin::coinData($row['coin_id'])['coin_name'];
        }
        if(isset($row['create_time'])){
            $row['create_date'] = date("Y-m-d H:i",$row['create_time']);
        }
        return $row;
    }

}